package com.central.apps.service.impl;

import com.central.app.service.HandlerService;
import com.central.apps.service.TaskStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 任务统计接口实现类
 *
 * @author 李大超
 * @date 2023/1/30
 */
@Slf4j
@Service
public class TaskStatisticsServiceImpl implements TaskStatisticsService {

    @Autowired
    private HandlerService handlerService;

    /**
     * 任务统计
     *
     * @param proid
     */
    @Override
    public void taskStatistics(Integer proid) throws Exception {
        int totalTasks = 0;
        int taskTotalFinish = 0;
        int taskTotalOngoing = 0;
        int taskTotalDelay = 0;
        List<Map<String, Object>> list = handlerService.findBySql("select count(id) as totalTasks, ifnull(sum((case when taskstatus = '已完成' then 1 else 0 end)), 0) as taskTotalFinish, " +
                " ifnull(sum((case when taskstatus = '进行中' then 1 else 0 end)), 0) as taskTotalOngoing, " +
                " ifnull(sum((case when taskstatus = '已延期' then 1 else 0 end)), 0) as taskTotalDelay from gq_scheduleplantask where proid = "+ proid);

        if (CollectionUtils.isNotEmpty(list)) {
            Map<String, Object> map = list.get(0);
            totalTasks = MapUtils.getInteger(map, "totalTasks");
            taskTotalFinish = MapUtils.getInteger(map, "taskTotalFinish");
            taskTotalOngoing = MapUtils.getInteger(map, "taskTotalOngoing");
            taskTotalDelay = MapUtils.getInteger(map, "taskTotalDelay");

            handlerService.updateBySql("update gq_proaccount set totaltasks = " + totalTasks
                    + ", tasktotalfinish = " + taskTotalFinish
                    + ", tasktotalongoing = " + taskTotalOngoing
                    + ", tasktotaldelay = " + taskTotalDelay
                    + " where id = "+ proid);
        }
    }
}
